#ifndef __IWL_JS_ENGINE_H__
#define __IWL_JS_ENGINE_H__

#include <activscp.h>

/*#include <list>
using namespace std;
#pragma warning(disable : 4786)*/

class CIWLScriptHost;
class CIWLAutoConfigJSParser;

class CIWLScriptHost: public IActiveScriptSite, 
						public IActiveScriptSiteWindow
{
public:
	CIWLScriptHost(const char* pNamedItem, HWND hWnd = 0);
	virtual ~CIWLScriptHost();
	HRESULT CreateScriptEngine();//it will create deafult script engine(JS);
	HRESULT CreateScriptEngine(const char* EngineName);
	HRESULT ParseScript(const char* script);
	HRESULT ParseFile(const char* pszFileName);
	HRESULT RunScript();
	HRESULT StopScript();
	HRESULT CallFunction(const char* funcname, DISPPARAMS* params, VARIANT* result);

public:
	//IUnknown members
	STDMETHOD(QueryInterface)(REFIID riid,void** ppvObj);
	STDMETHOD_(unsigned long,AddRef)(void);
	STDMETHOD_(unsigned long,Release)();
	//IActiveScriptSite members
	STDMETHOD(GetLCID)(LCID  *plcid) ;
	STDMETHOD(GetItemInfo)(LPCOLESTR pstrName,DWORD dwReturnMask,
							IUnknown  * *ppiunkItem,ITypeInfo  * *ppti) ;
	STDMETHOD(GetDocVersionString)(BSTR  *pbstrVersion) ;
	STDMETHOD(OnScriptTerminate)(const VARIANT  *pvarResult,const EXCEPINFO  *pexcepinfo) ;
	STDMETHOD(OnStateChange)(SCRIPTSTATE ssScriptState) ;
	STDMETHOD(OnScriptError)(IActiveScriptError  *pscripterror) ;
	STDMETHOD(OnEnterScript)(void) ;
	STDMETHOD(OnLeaveScript)(void) ;
	
	//IActiveScriptSiteWindow members
	STDMETHOD(GetWindow)(HWND *phwnd);
	STDMETHOD(EnableModeless)(BOOL);

protected:
	HRESULT CreateEngine(CLSID& engineCLSID);
	
private:
	IActiveScript* m_ps;
	IActiveScriptParse* m_psp;
private:
	LONG m_cref;
	char* m_pNamedItem;
	HWND m_hWnd;
};

class CIWLAutoConfigJSParser
{
public:
	CIWLAutoConfigJSParser();
	~CIWLAutoConfigJSParser();
	int Init();
	BOOL CanParserScript();
	int SetPacScript(const char* srcipt);
	int FindProxyFromURL(const char* url, const char* host, char*& proxyresult);

public:
	static LPCSTR GetLocalIPAddress();
	static LPCSTR ResolveIP(LPCSTR pHostName) ;
	
protected:
	CIWLScriptHost*		m_pScriptEngine;
	BOOL				m_bInitialized;
	BOOL				m_bSetPacScript;
#ifndef WIN32
	char*				m_strScript;
	char*				m_strAllScript;
	int					m_iAllLen;
#endif
};

#endif //!__IWL_JS_ENGINE_H__
